package com.das.mechanic_base.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

public class X3ChildRecyclerview extends RecyclerView {

    private float downX;
    private float downY;

    public X3ChildRecyclerview(@NonNull Context context) {
        super(context);
    }

    public X3ChildRecyclerview(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public X3ChildRecyclerview(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = ev.getX();
                downY = ev.getY();
                break;

            case MotionEvent.ACTION_MOVE:
                float upY = ev.getY();
                float upX = ev.getX();
                boolean b = canScrollVertically(-1);//是否滑动到顶部
                if(Math.abs(upY - downY) > Math.abs(upX - downX) && !b){
                    getParent().requestDisallowInterceptTouchEvent(false);
                }else if(Math.abs(upX - downX) > Math.abs(upY - downY)){
                    getParent().requestDisallowInterceptTouchEvent(false);
                }else {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }

                break;
        }
        return super.dispatchTouchEvent(ev);
    }
}